package com.example.homework.dao;

import static android.content.ContentValues.TAG;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.homework.Constants;
import com.example.homework.pojo.Note;

import java.util.ArrayList;

/**
 * @作者 zhaichenji
 * @创建日期 2022/4/12 16:54
 * @包名 com.example.homework.dao
 * @版本 1.0
 * @描述
 */
public class NoteDao extends MySQLiteOpenHelper {
    /**
     * 创建数据库
     *
     * @param context
     */
    public NoteDao(Context context) {
        super(context);
    }

    public long insertNote(Note note) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
//        values.put("note_creater",note.getNote_creater());

//        values.put("note_type",note.getNote_type());
        values.put("note_text", note.getNote_text());
        values.put("note_creater", note.getNote_creater());
        values.put("note_create_time", note.getNote_create_time());
//        values.put("note_pic",note.getNote_pic());
        long insert = db.insert(Constants.TABLE_NAME_NOTE, null, values);
        return insert;

    }

    public ArrayList<String> queryNote(String creater) {
        ArrayList<String> list = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        String note_text;
        String id;
        Cursor cursor = db.query(Constants.TABLE_NAME_NOTE, new String[]{"id", "note_text"},
                "note_creater=?", new String[]{creater}, null, null, null);
        while (cursor.moveToNext()) {
            note_text = cursor.getString(1);
            id = cursor.getString(0);
            list.add(id);
            list.add(note_text);
            Log.i(TAG, "queryNote: " + id + "<>" + note_text);
            Log.i(TAG, "queryNote: " + list);
        }
        return list;
    }
}
